package com.inspinia.config.common.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ResourceBundle;

/**
 * Created by veblen on 2016/9/12.
 */
@Slf4j
@Data
public class RoleConfig {

    /**
     * db.properties的配置数据
     */
    public static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("role");
    public static String getString(String key) {
        return RESOURCE_BUNDLE.getString(key);
    }

    public static String superUser = getString("super");

    /**
     *  监听配置
     * @param changeEvent
     */
    @ApolloConfigChangeListener("role")
    public void redisChange(ConfigChangeEvent changeEvent) {
        log.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType());
            if ("super".equals(change.getPropertyName())) {
                superUser = change.getNewValue();
            }
        }
    }
}
